<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Loanorderquery extends CI_Controller {
	
	private $tab ='account';
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}
	
	
	function tgzhuanzhang(){
    	$loanno 	= $_REQUEST['loanno'];
    	$orderno 	= $_REQUEST['orderno'];
    	$batchno 	= $_REQUEST['batchno'];
    	$bstime 	= $_REQUEST['strtime'];
    	$betime 	= $_REQUEST['endtime'];
    	$astime 	= $_REQUEST['strtime'];
    	$aetime 	= $_REQUEST['endtime'];
    	
    	$bstime = str_replace("-", "", $bstime);
    	$betime = str_replace("-", "", $betime);
    	if($bstime == ""){
    		$bstime = date('Ymd').'000000';
    	}else{
    		$bstime = $bstime.'000000';
    	}
		if($betime == ""){
    		$betime = date('Ymd',time()+24*60*60).'235959';
    	}else{
    		$betime = $betime.'235959';
    	}
		$list = self::loanorderquery("", $loanno, $orderno, $batchno, $bstime, $betime);
		$vdata['loanorder_list'] = $list;
		$vdata['strtime'] = "";
		if($astime != ""){
			$vdata['strtime'] = $astime;
		}
		$vdata['endtime'] = "";
		if($betime != ""){
			$vdata['endtime'] = $aetime;
		}
		$vdata['loanno'] = $loanno;
		$vdata['orderno'] = $orderno;
		$vdata['batchno'] = $batchno;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub']= 'tgzhuanzhang';
		$this->load->view('/ychadmin/loanorderquery_loan', $vdata);
	}

	function tgrecharge(){
		$loanno 	= $_REQUEST['loanno'];
    	$orderno 	= $_REQUEST['orderno'];
    	$bstime 	= $_REQUEST['strtime'];
    	$betime 	= $_REQUEST['endtime'];
    	$astime 	= $_REQUEST['strtime'];
    	$aetime 	= $_REQUEST['endtime'];
    	
    	$bstime = str_replace("-", "", $bstime);
    	$betime = str_replace("-", "", $betime);
    	if($bstime == ""){
    		$bstime = date('Ymd').'000000';
    	}else{
    		$bstime = $bstime.'000000';
    	}
		if($betime == ""){
    		$betime = date('Ymd',time()+24*60*60).'235959';
    	}else{
    		$betime = $betime.'235959';
    	}
		$list = self::loanorderquery("1", $loanno, $orderno, "", $bstime, $betime);
		$vdata['loanorder_list'] = $list;
		$vdata['strtime'] = "";
		if($astime != ""){
			$vdata['strtime'] = $astime;
		}
		$vdata['endtime'] = "";
		if($betime != ""){
			$vdata['endtime'] = $aetime;
		}
		$vdata['loanno'] = $loanno;
		$vdata['orderno'] = $orderno;
		$vdata['tab'] = $this->tab;
		$vdata['sub']= 'tgrecharge';
		
		$this->load->view('/ychadmin/loanorderquery_recharge', $vdata);
	}
	function tgcash(){
		$loanno 	= $_REQUEST['loanno'];
    	$orderno 	= $_REQUEST['orderno'];
    	$bstime 	= $_REQUEST['strtime'];
    	$betime 	= $_REQUEST['endtime'];
    	$astime 	= $_REQUEST['strtime'];
    	$aetime 	= $_REQUEST['endtime'];
    	
    	$bstime = str_replace("-", "", $bstime);
    	$betime = str_replace("-", "", $betime);
    	if($bstime == ""){
    		$bstime = date('Ymd').'000000';
    	}else{
    		$bstime = $bstime.'000000';
    	}
		if($betime == ""){
    		$betime = date('Ymd',time()+24*60*60).'235959';
    	}else{
    		$betime = $betime.'235959';
    	}
		$list = self::loanorderquery("2", $loanno, $orderno, "", $bstime, $betime);
		$vdata['loanorder_list'] = $list;
		$vdata['strtime'] = "";
		if($astime != ""){
			$vdata['strtime'] = $astime;
		}
		$vdata['endtime'] = "";
		if($betime != ""){
			$vdata['endtime'] = $aetime;
		}
		$vdata['loanno'] = $loanno;
		$vdata['orderno'] = $orderno;
		$vdata['tab'] = $this->tab;
		$vdata['sub']= 'tgcash';
		
		$this->load->view('/ychadmin/loanorderquery_cash', $vdata);
	}
	
	public function loanorderquery($qtype, $loanno, $orderno, $batchno, $stime, $etime){
		$this->load->library('/mmm/Rsa', 'rsa');
		$this->load->library('/mmm/Common', 'common');
		
		$PlatformMoneymoremore = MONEYMOREMOREPID;
		$Action = $qtype;
		$LoanNo = $loanno;
		$OrderNo = $orderno;
		$BatchNo = $batchno;
		$BeginTime = $stime;
		$EndTime = $etime;
		
		$dataStr = $PlatformMoneymoremore.$Action.$LoanNo.$OrderNo.$BatchNo.$BeginTime.$EndTime;
		if($this->common->getAntiState() == 1){
			$dataStr = strtoupper(md5($dataStr));
		}
		$SignInfo=$this->rsa->sign($dataStr);
	
		$officialAction = MONEYMOREMOREURL."/loan/loanorderquery.action";
		
		$post_data = array();  
		$post_data['PlatformMoneymoremore'] = $PlatformMoneymoremore;  
		$post_data['Action'] = $Action;  
		$post_data['LoanNo'] = $LoanNo;
		$post_data['OrderNo'] = $OrderNo;
		$post_data['BatchNo'] = $BatchNo;
		$post_data['BeginTime'] = $BeginTime;
		$post_data['EndTime'] = $EndTime;
		$post_data['SignInfo'] = $SignInfo;
		
		$url=$officialAction;  
		$o="";  
		foreach ($post_data as $k=>$v){  
		    $o.= "$k=".urlencode($v)."&";  
		}  
		$post_data=substr($o,0,-1);  
		$ch = curl_init();  
		curl_setopt($ch, CURLOPT_POST, 1);  
		curl_setopt($ch, CURLOPT_HEADER, 0);  
		curl_setopt($ch, CURLOPT_URL,$url);  
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
		//为了支持cookie  
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');  
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
		$ret = curl_exec($ch);  
		curl_close($ch);
		$jdate = substr($ret, 1, strlen($ret) - 2);
		$jdataarray = array();
		if(strpos($jdate, "},{")){
			$jdataarray = explode('},{', $jdate);	
		}else{
			$jdataarray[] = $jdate;
		}
		$index = 0;
		$account_list = array();
		foreach ($jdataarray as $ja){
			if(count($jdataarray) > 1){
				if($index == 0){
					$account_list[] = json_decode($ja."}", true);
				}else{
					$account_list[] = json_decode("{".$ja."}", true);
				}
			}elseif(count($jdataarray)== 1){
				//print_r(json_decode($ja, true));exit;
				$account_list[] = json_decode($ja, true);
			}
			$index++;
		}
		//print_r($account_list);exit;
		return $account_list;
	}
	
	function precharge(){
		if($this->input->post("act") == "act"){
			$money = $this->input->post("money");
			self::toloanrecharge($money);
		}else{
			$this->load->view('/ychadmin/precharge');
		}
	}
	
 public function toloanrecharge($money) {
        $this->load->library('/mmm/Rsa', 'rsa');
        $this->load->library('/mmm/Common', 'common');

        $userObj = $this->session->userdata('ych_user');
        $this->load->model("User_model");
        $user = $this->User_model->get_by_uid($userObj['user_id']);


        $RechargeMoneymoremore = MONEYMOREMOREPID;
        $PlatformMoneymoremore = MONEYMOREMOREPID;
        $OrderNo = date("Ymd");
        $Amount = $money;
        $RechargeType = "";
        $FeeType = "";
        $CardNo = "";
        $common = new Common();
        if ($common->getAntiState() == 0) {
            $RandomTimeStamp = "";
        } else if ($common->getAntiState() == 1) {
            $RandomTimeStamp = $common->getRandomTimeStamp();
        }
        $Remark1 = "";
        $Remark2 = "";
        $Remark3 = "";
        $ReturnURL = "http://" . $_SERVER["HTTP_HOST"] . "/moneymoremore/rechargereturn.html";
        $NotifyURL = "http://" . $_SERVER["HTTP_HOST"] . "/moneymoremore/rechargenotify.html";

        $dataStr = $RechargeMoneymoremore . $PlatformMoneymoremore . $OrderNo . $Amount . $RechargeType . $FeeType . $CardNo . $ReturnURL . $NotifyURL;
        if ($common->getAntiState() == 1) {
            $dataStr = strtoupper(md5($dataStr));
        }
        $rsa = new RSA();
        $SignInfo = $rsa->sign($dataStr);

        if (!empty($CardNo)) {
            $CardNo = $rsa->encrypt($CardNo);
        }
        $officialAction = MONEYMOREMOREURL . "/loan/toloanrecharge.action";

        echo "<form id=\"formu\" name=\"formu\" action=\"" . $officialAction . "\" method=\"post\">";
        echo "<input id=\"RechargeMoneymoremore\" name=\"RechargeMoneymoremore\" value=\"" . $RechargeMoneymoremore . "\" type=\"hidden\" />";
        echo "<input id=\"PlatformMoneymoremore\" name=\"PlatformMoneymoremore\" value=\"" . $PlatformMoneymoremore . "\" type=\"hidden\" />";
        echo "<input id=\"OrderNo\" name=\"OrderNo\" value=\"" . $OrderNo . "\" type=\"hidden\" />";
        echo "<input id=\"Amount\" name=\"Amount\" value=\"" . $Amount . "\" type=\"hidden\" />";
        echo "<input id=\"RechargeType\" name=\"RechargeType\" value=\"" . $RechargeType . "\" type=\"hidden\" />";
        echo "<input id=\"FeeType\" name=\"FeeType\" value=\"" . $FeeType . "\" type=\"hidden\" />";
        echo "<input id=\"CardNo\" name=\"CardNo\" value=\"" . $CardNo . "\" type=\"hidden\" />";
        echo "<input id=\"RandomTimeStamp\" name=\"RandomTimeStamp\" value=\"" . $RandomTimeStamp . "\" type=\"hidden\" />";
        echo "<input id=\"Remark1\" name=\"Remark1\" value=\"" . $Remark1 . "\" type=\"hidden\" />";
        echo "<input id=\"Remark2\" name=\"Remark2\" value=\"" . $Remark2 . "\" type=\"hidden\" />";
        echo "<input id=\"Remark3\" name=\"Remark3\" value=\"" . $Remark3 . "\" type=\"hidden\" />";
        echo "<input id=\"ReturnURL\" name=\"ReturnURL\" value=\"" . $ReturnURL . "\" type=\"hidden\" />";
        echo "<input id=\"NotifyURL\" name=\"NotifyURL\" value=\"" . $NotifyURL . "\" type=\"hidden\" />";
        echo "<input id=\"SignInfo\" name=\"SignInfo\" value=\"" . $SignInfo . "\" type=\"hidden\" />";
        echo "<script language=\"javascript\">window.onload=function(){document.formu.submit();}</script>";
        echo "</form>";
    }
}